home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / abbrev.el.z / abbrev.el
Encoding:
Text File  |  1998-05-21  |  19.4 KB  |  533 lines

  1. ;;; abbrev.el --- abbrev mode commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: abbrev
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  22. ;; 02111-1307, USA.
  23.  
  24. ;;; Synched up with: FSF 19.34 (With some additions)
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This facility is documented in the Emacs Manual.
  29.  
  30. ;;; Code:
  31.  
  32. ;jwz: this is preloaded so don't ;;;###autoload
  33. (defcustom only-global-abbrevs nil "\
  34. *Non-nil means user plans to use global abbrevs only.
  35. Makes the commands to define mode-specific abbrevs define global ones instead."
  36.   :type 'boolean
  37.   :group 'abbrev)
  38.  
  39. ;;; XEmacs: the following block of code is not in FSF
  40. (defvar abbrev-table-name-list '()
  41.   "List of symbols whose values are abbrev tables.")
  42.  
  43. (defvar abbrevs-changed nil
  44.   "Set non-nil by defining or altering any word abbrevs.
  45. This causes `save-some-buffers' to offer to save the abbrevs.")
  46.  
  47. (defun make-abbrev-table ()
  48.   "Create a new, empty abbrev table object."
  49.   (make-vector 59 0)) ; 59 is prime
  50.  
  51. (defun clear-abbrev-table (table)
  52.   "Undefine all abbrevs in abbrev table TABLE, leaving it empty."
  53.   (fillarray table 0)
  54.   (setq abbrevs-changed t)
  55.   nil)
  56.  
  57.  
  58. (defun define-abbrev-table (name defs)
  59.   "Define TABNAME (a symbol) as an abbrev table name.
  60. Define abbrevs in it according to DEFINITIONS, which is a list of elements
  61. of the form (ABBREVNAME EXPANSION HOOK USECOUNT)."
  62.   (let ((table (and (boundp name) (symbol-value name))))
  63.     (cond ((vectorp table))
  64.           ((not table)
  65.            (setq table (make-abbrev-table))
  66.            (set name table)
  67.            (setq abbrev-table-name-list (cons name abbrev-table-name-list)))
  68.           (t
  69.            (setq table (signal 'wrong-type-argument (list 'vectorp table)))
  70.            (set name table)))
  71.     (while defs
  72.       (apply (function define-abbrev) table (car defs))
  73.       (setq defs (cdr defs)))))
  74.  
  75. (defun define-abbrev (table name &optional expansion hook count)
  76.   "Define an abbrev in TABLE named NAME, to expand to EXPANSION or call HOOK.
  77. NAME and EXPANSION are strings.  Hook is a function or `nil'.
  78. To undefine an abbrev, define it with an expansion of `nil'."
  79.   (or (not expansion)
  80.       (stringp expansion)
  81.       (setq expansion (signal 'wrong-type-argument
  82.                               (list 'stringp expansion))))
  83.   (or (not count)
  84.       (integerp count)
  85.       (setq count (signal 'wrong-type-argument
  86.                           (list 'fixnump count))))
  87.   (or (vectorp table)
  88.       (setq table (signal 'wrong-type-argument
  89.               (list 'vectorp table))))
  90.   (let* ((sym (intern name table))
  91.          (oexp (and (boundp sym) (symbol-value sym)))
  92.          (ohook (and (fboundp sym) (symbol-function sym))))
  93.     (unless (and (equal ohook hook)
  94.          (stringp oexp)
  95.          (stringp expansion)
  96.          (string-equal oexp expansion))
  97.       (setq abbrevs-changed t)
  98.       ;; If there is a non-word character in the string, set the flag.
  99.       (if (string-match "\\W" name)
  100.       (set (intern " " table) nil)))
  101.     (set sym expansion)
  102.     (fset sym hook)
  103.     (setplist sym (or count 0))
  104.     name))
  105.  
  106.  
  107. ;; Fixup stuff from bootstrap def of define-abbrev-table in subr.el
  108. (let ((l abbrev-table-name-list))
  109.   (while l
  110.     (let ((fixup (car l)))
  111.       (if (consp fixup)
  112.           (progn
  113.             (setq abbrev-table-name-list (delq fixup abbrev-table-name-list))
  114.             (define-abbrev-table (car fixup) (cdr fixup))))
  115.       (setq l (cdr l))))
  116.   ;; These are no longer initialised by C code
  117.   (if (not global-abbrev-table)
  118.       (progn
  119.         (setq global-abbrev-table (make-abbrev-table))
  120.         (setq abbrev-table-name-list (cons 'global-abbrev-table
  121.                                            abbrev-table-name-list))))
  122.   (if (not fundamental-mode-abbrev-table)
  123.       (progn
  124.         (setq fundamental-mode-abbrev-table (make-abbrev-table))
  125.         (setq abbrev-table-name-list (cons 'fundamental-mode-abbrev-table
  126.                                            abbrev-table-name-list))))
  127.   (and (eq major-mode 'fundamental-mode)
  128.        (not local-abbrev-table)
  129.        (setq local-abbrev-table fundamental-mode-abbrev-table)))
  130.  
  131.  
  132. (defun define-global-abbrev (name expansion)
  133.   "Define ABBREV as a global abbreviation for EXPANSION."
  134.   (interactive "sDefine global abbrev: \nsExpansion for %s: ")
  135.   (define-abbrev global-abbrev-table
  136.                  (downcase name) expansion nil 0))
  137.  
  138. (defun define-mode-abbrev (name expansion)
  139.   "Define ABBREV as a mode-specific abbreviation for EXPANSION."
  140.   (interactive "sDefine mode abbrev: \nsExpansion for %s: ")
  141.   (define-abbrev (or local-abbrev-table
  142.                      (error "Major mode has no abbrev table"))
  143.          (downcase name) expansion nil 0))
  144.  
  145. (defun abbrev-symbol (abbrev &optional table)
  146.   "Return the symbol representing abbrev named ABBREV.
  147. This symbol's name is ABBREV, but it is not the canonical symbol of that name;
  148. it is interned in an abbrev-table rather than the normal obarray.
  149. The value is nil if that abbrev is not defined.
  150. Optional second arg TABLE is abbrev table to look it up in.
  151. The default is to try buffer's mode-specific abbrev table, then global table."
  152.   (let ((frob (function (lambda (table)
  153.                 (let ((sym (intern-soft abbrev table)))
  154.                   (if (and (boundp sym)
  155.                            (stringp (symbol-value sym)))
  156.                       sym
  157.                       nil))))))
  158.     (if table
  159.         (funcall frob table)
  160.         (or (and local-abbrev-table
  161.                  (funcall frob local-abbrev-table))
  162.             (funcall frob global-abbrev-table)))))
  163.  
  164. (defun abbrev-expansion (abbrev &optional table)
  165.   "Return the string that ABBREV expands into in the current buffer.
  166. Optionally specify an abbrev table as second arg;
  167. then ABBREV is looked up in that table only."
  168.   (let ((sym (abbrev-symbol abbrev table)))
  169.     (if sym
  170.         (symbol-value sym)
  171.         nil)))
  172.  
  173. (defun unexpand-abbrev ()
  174.   "Undo the expansion of the last abbrev that expanded.
  175. This differs from ordinary undo in that other editing done since then
  176. is not undone."
  177.   (interactive) 
  178.   (if (or (< last-abbrev-location (point-min))
  179.           (> last-abbrev-location (point-max))
  180.           (not (stringp last-abbrev-text)))
  181.       nil
  182.     (let* ((opoint (point))
  183.            (val (symbol-value last-abbrev))
  184.            (adjust (length val)))
  185.       ;; This isn't correct if (symbol-function last-abbrev-text)
  186.       ;;  was used to do the expansion
  187.       (goto-char last-abbrev-location)
  188.       (delete-region last-abbrev-location (+ last-abbrev-location adjust))
  189.       (insert last-abbrev-text)
  190.       (setq adjust (- adjust (length last-abbrev-text)))
  191.       (setq last-abbrev-text nil)
  192.       (if (< last-abbrev-location opoint)
  193.           (goto-char (- opoint adjust))
  194.           (goto-char opoint)))))
  195.  
  196.  
  197.  
  198. (defun insert-abbrev-table-description (name human-readable)
  199.   "Insert before point a full description of abbrev table named NAME.
  200. NAME is a symbol whose value is an abbrev table.
  201. If optional 2nd arg HUMAN is non-nil, insert a human-readable description.
  202. Otherwise the description is an expression,
  203. a call to `define-abbrev-table', which would
  204. define the abbrev table NAME exactly as it is currently defined."
  205.   (let ((table (symbol-value name))
  206.         (stream (current-buffer)))
  207.     (message "Abbrev-table %s..." name) 
  208.     (if human-readable
  209.         (progn
  210.           (prin1 (list name) stream)
  211.           ;; Need two terpri's or cretinous edit-abbrevs blows out
  212.           (terpri stream)
  213.           (terpri stream)
  214.           (mapatoms (function (lambda (sym)
  215.                       (if (symbol-value sym)
  216.                           (let* ((n (prin1-to-string (symbol-name sym)))
  217.                                  (pos (length n)))
  218.                             (princ n stream)
  219.                             (while (< pos 14)
  220.                               (write-char ?\  stream)
  221.                               (setq pos (1+ pos)))
  222.                             (princ (format " %-5S " (symbol-plist sym))
  223.                                    stream)
  224.                             (if (not (symbol-function sym))
  225.                                 (prin1 (symbol-value sym) stream)
  226.                               (progn
  227.                                 (setq n (prin1-to-string (symbol-value sym))
  228.                                       pos (+ pos 6 (length n)))
  229.                                 (princ n stream)
  230.                                 (while (< pos 45)
  231.                                   (write-char ?\  stream)
  232.                                   (setq pos (1+ pos)))
  233.                                 (prin1 (symbol-function sym) stream)))
  234.                             (terpri stream)))))
  235.                     table)
  236.           (terpri stream))
  237.         (progn
  238.           (princ "\(define-abbrev-table '" stream)
  239.           (prin1 name stream)
  240.           (princ " '\(\n" stream)
  241.           (mapatoms (function (lambda (sym)
  242.                       (if (symbol-value sym)
  243.                           (progn
  244.                             (princ "    " stream)
  245.                             (prin1 (list (symbol-name sym)
  246.                                          (symbol-value sym)
  247.                                          (symbol-function sym)
  248.                                          (symbol-plist sym))
  249.                                    stream)
  250.                             (terpri stream)))))
  251.                     table)
  252.           (princ "    \)\)\n" stream)))
  253.     (terpri stream))
  254.   (message ""))
  255. ;;; End code not in FSF
  256.  
  257. (defun abbrev-mode (arg)
  258.   "Toggle abbrev mode.
  259. With argument ARG, turn abbrev mode on iff ARG is positive.
  260. In abbrev mode, inserting an abbreviation causes it to expand
  261. and be replaced by its expansion."
  262.   (interactive "P")
  263.   (setq abbrev-mode
  264.     (if (null arg) (not abbrev-mode)
  265.       (> (prefix-numeric-value arg) 0)))
  266.   ;; XEmacs change
  267.   (redraw-modeline))
  268.  
  269.  
  270. (defvar edit-abbrevs-map nil
  271.   "Keymap used in edit-abbrevs.")
  272. (if edit-abbrevs-map
  273.     nil
  274.   (setq edit-abbrevs-map (make-sparse-keymap))
  275.   ;; XEmacs change
  276.   (set-keymap-name edit-abbrevs-map 'edit-abbrevs-map)
  277.   (define-key edit-abbrevs-map "\C-x\C-s" 'edit-abbrevs-redefine)
  278.   (define-key edit-abbrevs-map "\C-c\C-c" 'edit-abbrevs-redefine))
  279.  
  280. (defun kill-all-abbrevs ()
  281.   "Undefine all defined abbrevs."
  282.   (interactive)
  283.   (let ((tables abbrev-table-name-list))
  284.     (while tables
  285.       (clear-abbrev-table (symbol-value (car tables)))
  286.       (setq tables (cdr tables)))))
  287.  
  288. (defun insert-abbrevs ()
  289.   "Insert after point a description of all defined abbrevs.
  290. Mark is set after the inserted text."
  291.   (interactive)
  292.   (push-mark
  293.    (save-excursion
  294.     (let ((tables abbrev-table-name-list))
  295.       (while tables
  296.     (insert-abbrev-table-description (car tables) t)
  297.     (setq tables (cdr tables))))
  298.     (point))))
  299.  
  300. (defun list-abbrevs ()
  301.   "Display a list of all defined abbrevs."
  302.   (interactive)
  303.   (display-buffer (prepare-abbrev-list-buffer)))
  304.  
  305. (defun prepare-abbrev-list-buffer ()
  306.   (save-excursion
  307.     (set-buffer (get-buffer-create "*Abbrevs*"))
  308.     (erase-buffer)
  309.     (let ((tables abbrev-table-name-list))
  310.       (while tables
  311.     (insert-abbrev-table-description (car tables) t)
  312.     (setq tables (cdr tables))))
  313.     (goto-char (point-min))
  314.     (set-buffer-modified-p nil)
  315.     (edit-abbrevs-mode))
  316.   (get-buffer-create "*Abbrevs*"))
  317.  
  318. (defun edit-abbrevs-mode ()
  319.   "Major mode for editing the list of abbrev definitions.
  320. \\{edit-abbrevs-map}"
  321.   (interactive)
  322.   (setq major-mode 'edit-abbrevs-mode)
  323.   (setq mode-name "Edit-Abbrevs")
  324.   (use-local-map edit-abbrevs-map))
  325.  
  326. (defun edit-abbrevs ()
  327.   "Alter abbrev definitions by editing a list of them.
  328. Selects a buffer containing a list of abbrev definitions.
  329. You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
  330. according to your editing.
  331. Buffer contains a header line for each abbrev table,
  332.  which is the abbrev table name in parentheses.
  333. This is followed by one line per abbrev in that table:
  334. NAME   USECOUNT   EXPANSION   HOOK
  335. where NAME and EXPANSION are strings with quotes,
  336. USECOUNT is an integer, and HOOK is any valid function
  337. or may be omitted (it is usually omitted)."
  338.   (interactive)
  339.   (switch-to-buffer (prepare-abbrev-list-buffer)))
  340.  
  341. (defun edit-abbrevs-redefine ()
  342.   "Redefine abbrevs according to current buffer contents."
  343.   (interactive)
  344.   (define-abbrevs t)
  345.   (set-buffer-modified-p nil))
  346.  
  347. (defun define-abbrevs (&optional arg)
  348.   "Define abbrevs according to current visible buffer contents.
  349. See documentation of `edit-abbrevs' for info on the format of the
  350. text you must have in the buffer.
  351. With argument, eliminate all abbrev definitions except
  352. the ones defined from the buffer now."
  353.   (interactive "P")
  354.   (if arg (kill-all-abbrevs))
  355.   (save-excursion
  356.    (goto-char (point-min))
  357.    (while (and (not (eobp)) (re-search-forward "^(" nil t))
  358.      (let* ((buf (current-buffer))
  359.         (table (read buf))
  360.         abbrevs name hook exp count)
  361.        (forward-line 1)
  362.        (while (progn (forward-line 1)
  363.              (not (eolp)))
  364.      (setq name (read buf) count (read buf) exp (read buf))
  365.      (skip-chars-backward " \t\n\f")
  366.      (setq hook (if (not (eolp)) (read buf)))
  367.      (skip-chars-backward " \t\n\f")
  368.      (setq abbrevs (cons (list name exp hook count) abbrevs)))
  369.        (define-abbrev-table table abbrevs)))))
  370.  
  371. (defun read-abbrev-file (&optional file quietly)
  372.   "Read abbrev definitions from file written with `write-abbrev-file'.
  373. Optional argument FILE is the name of the file to read;
  374. it defaults to the value of `abbrev-file-name'.
  375. Optional second argument QUIETLY non-nil means don't print anything."
  376.   (interactive "fRead abbrev file: ")
  377.   (load (if (and file (> (length file) 0)) file abbrev-file-name)
  378.     nil quietly)
  379.   (setq save-abbrevs t abbrevs-changed nil))
  380.  
  381. (defun quietly-read-abbrev-file (&optional file)
  382.   "Read abbrev definitions from file written with write-abbrev-file.
  383. Optional argument FILE is the name of the file to read;
  384. it defaults to the value of `abbrev-file-name'.
  385. Does not print anything."
  386.   ;(interactive "fRead abbrev file: ")
  387.   (read-abbrev-file file t))
  388.  
  389. (defun write-abbrev-file (file)
  390.   "Write all abbrev definitions to a file of Lisp code.
  391. The file written can be loaded in another session to define the same abbrevs.
  392. The argument FILE is the file name to write."
  393.   (interactive
  394.    (list
  395.     (read-file-name "Write abbrev file: "
  396.             (file-name-directory (expand-file-name abbrev-file-name))
  397.             abbrev-file-name)))
  398.   (or (and file (> (length file) 0))
  399.       (setq file abbrev-file-name))
  400.   (save-excursion
  401.    (set-buffer (get-buffer-create " write-abbrev-file"))
  402.    (erase-buffer)
  403.    (let ((tables abbrev-table-name-list))
  404.      (while tables
  405.        (insert-abbrev-table-description (car tables) nil)
  406.        (setq tables (cdr tables))))
  407.    (write-region 1 (point-max) file)
  408.    (erase-buffer)))
  409.  
  410. (defun add-mode-abbrev (arg)
  411.   "Define mode-specific abbrev for last word(s) before point.
  412. Argument is how many words before point form the expansion;
  413. or zero means the region is the expansion.
  414. A negative argument means to undefine the specified abbrev.
  415. Reads the abbreviation in the minibuffer.
  416.  
  417. Don't use this function in a Lisp program; use `define-abbrev' instead."
  418.   ;; XEmacs change:
  419.   (interactive "P")
  420.   (add-abbrev
  421.    (if only-global-abbrevs
  422.        global-abbrev-table 
  423.      (or local-abbrev-table
  424.      (error "No per-mode abbrev table")))
  425.    "Mode" arg))
  426.  
  427. (defun add-global-abbrev (arg)
  428.   "Define global (all modes) abbrev for last word(s) before point.
  429. The prefix argument specifies the number of words before point that form the
  430. expansion; or zero means the region is the expansion.
  431. A negative argument means to undefine the specified abbrev.
  432. This command uses the minibuffer to read the abbreviation.
  433.  
  434. Don't use this function in a Lisp program; use `define-abbrev' instead."
  435.   ;; XEmacs change:
  436.   (interactive "P")
  437.   (add-abbrev global-abbrev-table "Global" arg))
  438.  
  439. (defun add-abbrev (table type arg)
  440.   ;; XEmacs change:
  441.   (if (and (not arg) (region-active-p)) (setq arg 0)
  442.     (setq arg (prefix-numeric-value arg)))
  443.   (let ((exp (and (>= arg 0)
  444.           (buffer-substring
  445.            (point)
  446.            (if (= arg 0) (mark)
  447.              (save-excursion (forward-word (- arg)) (point))))))
  448.     name)
  449.     (setq name
  450.       (read-string (format (if exp "%s abbrev for \"%s\": "
  451.                  "Undefine %s abbrev: ")
  452.                    type exp)))
  453.     (set-text-properties 0 (length name) nil name)
  454.     (if (or (null exp)
  455.         (not (abbrev-expansion name table))
  456.         (y-or-n-p (format "%s expands to \"%s\"; redefine? "
  457.                   name (abbrev-expansion name table))))
  458.     (define-abbrev table (downcase name) exp))))
  459.     
  460. (defun inverse-add-mode-abbrev (arg)
  461.   "Define last word before point as a mode-specific abbrev.
  462. With prefix argument N, defines the Nth word before point.
  463. This command uses the minibuffer to read the expansion.
  464. Expands the abbreviation after defining it."
  465.   (interactive "p")
  466.   (inverse-add-abbrev
  467.    (if only-global-abbrevs
  468.        global-abbrev-table 
  469.      (or local-abbrev-table
  470.      (error "No per-mode abbrev table")))
  471.    "Mode" arg))
  472.  
  473. (defun inverse-add-global-abbrev (arg)
  474.   "Define last word before point as a global (mode-independent) abbrev.
  475. With prefix argument N, defines the Nth word before point.
  476. This command uses the minibuffer to read the expansion.
  477. Expands the abbreviation after defining it."
  478.   (interactive "p")
  479.   (inverse-add-abbrev global-abbrev-table "Global" arg))
  480.  
  481. (defun inverse-add-abbrev (table type arg)
  482.   (let (name nameloc exp)
  483.     (save-excursion
  484.      (forward-word (- arg))
  485.      (setq name (buffer-substring (point) (progn (forward-word 1)
  486.                            (setq nameloc (point))))))
  487.     (set-text-properties 0 (length name) nil name)
  488.     (setq exp (read-string (format "%s expansion for \"%s\": "
  489.                    type name)))
  490.     (if (or (not (abbrev-expansion name table))
  491.         (y-or-n-p (format "%s expands to \"%s\"; redefine? "
  492.                   name (abbrev-expansion name table))))
  493.     (progn
  494.      (define-abbrev table (downcase name) exp)
  495.      (save-excursion
  496.       (goto-char nameloc)
  497.       (expand-abbrev))))))
  498.  
  499. (defun abbrev-prefix-mark (&optional arg)
  500.   "Mark current point as the beginning of an abbrev.
  501. Abbrev to be expanded starts here rather than at beginning of word.
  502. This way, you can expand an abbrev with a prefix: insert the prefix,
  503. use this command, then insert the abbrev."
  504.   (interactive "P")
  505.   (or arg (expand-abbrev))
  506.   (setq abbrev-start-location (point-marker)
  507.     abbrev-start-location-buffer (current-buffer))
  508.   (let ((e (make-extent (point) (point))))
  509.     (set-extent-begin-glyph e (make-glyph [string :data "-"]))))
  510.  
  511. (defun expand-region-abbrevs (start end &optional noquery)
  512.   "For abbrev occurrence in the region, offer to expand it.
  513. The user is asked to type y or n for each occurrence.
  514. A prefix argument means don't query; expand all abbrevs.
  515. If called from a Lisp program, arguments are START END &optional NOQUERY."
  516.   (interactive "r\nP")
  517.   (save-excursion
  518.     (goto-char start)
  519.     (let ((lim (- (point-max) end))
  520.       pnt string)
  521.       (while (and (not (eobp))
  522.           (progn (forward-word 1)
  523.              (<= (setq pnt (point)) (- (point-max) lim))))
  524.     (if (abbrev-expansion
  525.          (setq string
  526.            (buffer-substring
  527.             (save-excursion (forward-word -1) (point))
  528.             pnt)))
  529.         (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
  530.         (expand-abbrev)))))))
  531.  
  532. ;;; abbrev.el ends here
  533.